<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>分割平衡字符串</title>
  </head>
  <body>
    <script>
      var balancedStringSplit = function (s) {
        var sLength = s.length;
        var arr = "";
        var num = 0;
        for (let i = 0; i < sLength; i++) {
          if (arr == "" || arr[arr.length - 1] == s[i]) {
            arr += s[i];
          } else {
            arr = arr.slice(0, arr.length - 1);
            if (arr == "") {
              num++;
            }
          }
        }
        return num;
      };
      //------------方法二-------------
      // var balancedStringSplit = function (s) {
      //   let num = 0;
      //   let res = 0;
      //   for (let i = 0; i < s.length; i++) {
      //     s[i] == "L" ? num++ : num--;
      //     if (num == 0) res++;
      //   }
      //   return res;
      // };

      var s = "lrlrlrrrll";
      console.log(balancedStringSplit(s));
    </script>
  </body>
</html>
